package com.wcgksoft.controller.admin;

import com.wcgksoft.common.util.Layui;
import com.wcgksoft.common.util.comm.StringUtils;
import com.wcgksoft.entity.admin.Authority;
import com.wcgksoft.entity.goods.Coupon;
import com.wcgksoft.entity.question.AdviceType;
import com.wcgksoft.service.admin.CouponService;
import net.sf.json.JSONObject;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author 御风
 * @version V1.0
 * @Package com.wcgksoft.controller.admin
 * @Description TODO 优惠券
 * @date 2020/5/15 17:11
 * @ClassName CouponController
 */
@Controller
@RequestMapping("admin/coupon")
public class CouponController {

    @Autowired
    private CouponService couponService;

    /**
     * 查询所有优惠券
     * @param page
     * @param limit
     * @param couponName
     * @param time
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "findAll")
    public Layui findAll(
            @RequestParam(value = "page",required = false)Integer page,
            @RequestParam(value = "limit",required = false)Integer limit,
            @RequestParam(value = "couponName",required = false)String couponName
//            @RequestParam(value = "time",required = false)String time
    ){
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Coupon.class);
        if (StringUtils.isNotEmpty(couponName)){
            detachedCriteria.add(Restrictions.like("couponName",couponName, MatchMode.ANYWHERE));
        }
        List<Coupon> list = couponService.findByConditions(detachedCriteria, page, limit);
        return Layui.data(0, "success", couponService.getTotalCount(), list);
    }


    /**
     * 增加和修改优惠券
     * @param data
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "saveCoupon")
    public Layui update(@RequestParam(value = "data",required = false)String data){
        JSONObject param = JSONObject.fromObject(data);
        System.out.println(param);
        Coupon coupon=null;
        if (null==param){
            return Layui.data(1, "请求格式有误", 0, null);
        }else{
            coupon=(Coupon) JSONObject.toBean(JSONObject.fromObject(data), Coupon.class);
            couponService.save(coupon);
        }
        return Layui.data(0,"操作成功",couponService.getTotalCount(),Layui.addList(coupon));
    }

    @ResponseBody
    @RequestMapping(value = "delete")
    public Layui delete(@RequestParam(value = "id",required = false) Integer id){
        try {
            Coupon byId = couponService.findById(id);
            couponService.delete(byId);
            return Layui.data(0,"操作成功",0,null);
        }catch (Exception e){
            e.printStackTrace();
            return Layui.data(0,"删除失败",0,null);
        }

    }




}
